package cn.gzsxt.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.gzsxt.mapper.DictionaryMapper;
import cn.gzsxt.mapper.ModularMapper;
import cn.gzsxt.mapper.PermissionMapper;
import cn.gzsxt.service.PermissionService;
import cn.gzsxt.utils.Page;

@Service
public class PermissionServiceImpl implements PermissionService {
	
	@Autowired
	private DictionaryMapper dictionaryMapper;
	@Autowired
	private ModularMapper modularMapper;
	@Autowired
	private PermissionMapper permissionMapper;

	@Override
	public Map<String, Object> addPermission(Map<String, Object> permission) {
		int count = permissionMapper.insert(permission);
		if(count>0){
			return permission;
		}
		return null;
	}

	@Override
	public int deletePermissionByIds(Object... permissionIds) {
		return permissionMapper.deleteByIds(permissionIds);
	}

	@Override
	public Map<String, Object> editPermission(Map<String, Object> permission) {
		int count = permissionMapper.updateForNotnull(permission);
		if (count>0) {
			return permission;
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> findAllPermission() {
		return permissionMapper.findAll();
	}

	@Override
	public Page findPermissionByConditionToPage(Map<String, Object> condition, int index, int size) {
		//构建一个分页对象
		//1.获得总记录数据
		long count = permissionMapper.count(condition);
		//2.获得分页数据
		int start=index*size;
		List<Map<String, Object>> permissions = permissionMapper.findByConditionAndPage(condition, start, size);
		for (Map<String, Object> permission : permissions) {
			//将模块记录拼接到权限里面
			Object modularId = permission.get("MODULAR_ID");
			Map<String, Object> modular = modularMapper.findById(modularId);
			permission.put("modular", modular);
			//通过数据字典，获得显示状态
			Map<String, Object> isShow = dictionaryMapper.findByTypeCodeAndValue("1002", permission.get("PERMISSION_IS_SHOW"));
			permission.put("PERMISSION_IS_SHOW", isShow.get("DICTIONARY_NAME"));
			
			//通过数据字典，获得权限类型
			Map<String, Object> type = dictionaryMapper.findByTypeCodeAndValue("1003", permission.get("PERMISSION_TYPE"));
			permission.put("PERMISSION_TYPE", type.get("DICTIONARY_NAME"));
			
		}
		
		
		//3.构建分页对象
		Page page=new Page(index, size, count, permissions);
		//4.返回分页对象
		return page;
	}

	@Override
	public Map<String, Object> findPermissionById(Object permissionId) {
		
		return permissionMapper.findById(permissionId);
	}

}
